<?php
include 'D:/dingjiangbbs/common/home.php';
//屏蔽notice错误
error_reporting(E_ALL ^ E_NOTICE);

$uid = $_COOKIE['uid'];
//var_dump($_GET);
$id  = $_GET['id'];
//var_dump($id);
$res1 = select($link,DB_TABLE_DETAIL,'*',"where id=$id");
//var_dump($res1);
$money = $res1[0]['rate'];
$classid = $res1[0]['classid'];

/*******************购买帖子操作*********************************/
//判断当前用户是否已经购买
if ($_GET['order'] == '购买') {

   //用id查询出信息
   $res = select($link,DB_TABLE_DETAIL,'*',"where id=$id");
	//var_dump($res);
	$classid  = $res[0]['classid'];
	$rate     = $res[0]['rate'];
	$uid_shop = $res[0]['authorid'];
	//var_dump($rate,$uid_shop,$uid_shop);

	//若帖子售价不为0,则需要进行购买操作
	if ($rate != 0) {
		
		//查看购买记录是否存在当前用户,帖子id和用户uid必须同时满足
		$res_sel = select($link,DB_TABLE_ORDER,'*',"where tid=$id and uid=$uid");
		//var_dump($res_sel);

		//判断是否购买
		if (!$res_sel) {

			//根据uid查看用户信息
			$res_user = select($link,DB_TABLE_USER,'*',"where uid=$uid");
			$grade_u  = $res_user[0]['grade'];
			var_dump($grade_u);

			//判断当前积分是否充足
			if ($grade_u < $rate) {

				exit('<a href="'.WEB_SITE.'public.php">当前余额不足,无法购买,点击返回帖子公共栏</a>');
			}

	/***************给买家减积分*********************/
			//根据买家积分判断买家的等级称号
			$grade_u2 = $grade_u - $rate;

			for ($m=1;$m<=10;$m++) {
						
				if ($grade_u2 >= $m-100  && $grade_u2 < $m.'00') {

					$levname_u = 'LV'.$m;
					//var_dump($levname_u);
					break;
				}
			}
			//var_dump($levname_u);
			
			//扣买家积分,修改买家信息
			$data_pay = [
				'grade'   => $grade_u2,
				'levname' => "$levanme_u"
			];
			//var_dump($data_pay);
			$res_pay = update($link,DB_TABLE_USER,$data_pay,"uid=$uid");
			//var_dump($res_pay);
			

		/***************给卖家加积分*********************/
			//查询卖家信息,给卖家加积分
			$res_s1    = select($link,DB_TABLE_USER,'*',"where uid=$uid_shop");
			$grade_s1  = $res_s1[0]['grade'];
			//var_dump($grade_s1);
			$grade_s2  = $grade_s1 + $rate;
			//var_dump($grade_s2);
			
			//根据买家积分判断买家的等级称号
			for ($m=1;$m<=10;$m++) {
						
				if ($grade_s2 >= $m-100  && $grade_s2 < $m.'00') {

					$levname_s = 'LV'.$m;
					//var_dump($levname_s);
					break;
				}
			}
			//var_dump($levname_s);
			
			//给卖家加积分
			$data_s1 = [
				'grade'   => $grade_s2,
				'levname' => "$levname_s"
			];
			$res_s1  = update($link,DB_TABLE_USER,$data_s1,"uid=$uid_shop");
			//var_dump($res_s1);

		/*************插入一条购买记录******************/
			//order表插入一条购买记录
			$data_buy = [

				'rate'    => $rate,
				'uid'     => $uid,
				'ispay'   => 1,
				'addtime' => time(),
				'tid'     => $id
			];

			$res_buy  = insert($link,DB_TABLE_ORDER,$data_buy,"uid=$uid");
			//var_dump($res_buy);
			echo '恭喜您购买成功!!!!<br /><br />';
		}

	}

	echo '欢迎查看,记得常来哟';
	header('refresh:3;url='.WEB_SITE.'personal.php?id='.$id);

}else if ($_GET['order'] == '返回') {

	//不购买,返回公共页面
	echo '正在返回';
	header('refresh:3;url='.WEB_SITE.'public.php?classid='.$classid);

}else {

	//判断帖子售价是否为0
	if ($money < 1 ) {
		
		//若售价为0,直接进入个人展示页面
		echo '等待跳转';
		header('refresh:1;url='.WEB_SITE.'personal.php?id='.$id);
	
	}else{

		//若帖子售价不为0 ,返回此页面进行购买操作
		echo '帖子需要<span style="color:red;font-weight:bold;">&nbsp;'.$money.'&nbsp;</span>积分购买才能观看,是否购买??<br /><br />提示:<br />若已经购买,请点击购买,不会重复扣费,请放心使用';

		//按钮,form表单跳转,get传id ,classid的值
		echo '<form action="admin_cg_post_buy.php?" method="get" enctype="multipart/form-data" >
			<input type="hidden" name="id" value="'.$id.'">
			<input type="hidden" name="classid" value="'.$classid.'">
			<input style="margin-left:40px; margin-top:20px;color:tomato;font-weight:bold;" type="submit" name="order" value="购买">

			<input style="margin-left:40px; margin-top:20px;color:#7B68EE;font-weight:bold;" type="submit" name="order" value="返回">

			</form>';

	}


}

/*************购买帖子操作结束*********************/
/*
	echo '<a href="'.WEB_SITE.'personal.php?id='.$id.'">是,购买继续观看</a><br />';
	echo '<a href="'.WEB_SITE.'public.php?classid='.$classid.'">否,返回上一页面</a>';
*/


